#include <vector>
#include <map>
#include "sched_lottery.h"
#include "basesched.h"
#include "stdio.h"

using namespace std;

int base_ticket;

SchedLottery::SchedLottery(std::vector<int> argn) {
	def_quantum = argn[0];
	quantum = 0;
	srand(argn[1]);
	cant_tickets = 0;
	base_ticket = 10;
}

void SchedLottery::load(int pid) {
/*	ticketsYpids up;
	ti.pid = pid;
	ti.ticket = 10;
	ti.block = false;
	uploaded.push_back(up);
	ready.push_back(up);
*/	
	ready.push_back(pid);
	//tickets.insert(pair<int,int>(pid, 10));
	tickets.insert(std::pair<int,int>(pid, base_ticket));
	cant_tickets += base_ticket;
}

void SchedLottery::unblock(int pid) {
	//~ for(unsigned int i=0; i<ready.size(); i++){
		//~ if(ready[i] == pid){
			//~ ready.erase(ready.begin()+i);
			//~ break;
		//~ }
	//~ }
	//printf("UNBLOCK PID: %i --- TICKETS: %i", pid, tickets[pid]);
	cant_tickets += tickets[pid];
	//printf("--- CANT_TICKETS: %i\n", cant_tickets);
	ready.push_back(pid); 
}

int SchedLottery::tick(const enum Motivo m) {
	//printf(
	if(m == EXIT) {
		//printf("EXIT PID: %i --- CANT_TICKETS: %i --- TICKETS: %i\n", current_pid(), cant_tickets, tickets[current_pid()]);
		cant_tickets -= tickets[current_pid()];
		//borro el pid de ready 
		for(unsigned int i=0; i<ready.size(); i++){
			if(ready[i] == current_pid()){
				//ready.erase(i);
				ready.erase(ready.begin()+i);
				break;
			}
		}
		//lo borro de tickets
		tickets.erase(current_pid());
		return siguiente();
		
	}else if (m == BLOCK){
		//borro el pid de ready 
		for(unsigned int i=0; i<ready.size(); i++){
			if(ready[i] == current_pid()){
				ready.erase(ready.begin()+i);
				break;
				//FUNCION
			}
		}
		

		cant_tickets -= tickets[current_pid()];
		compensationT();
		quantum = def_quantum;
		return siguiente();
		
	}else if(quantum != 0){
		quantum--;
		
	}else {
		//cambio a la tarea siguiente
		//compensationT(); 
		
		quantum = def_quantum;
		return siguiente();
	}
	
	return current_pid();
}
	
int SchedLottery::siguiente(){
	if(ready.size() != 0){
		int randomito = rand() % cant_tickets; 
		//printf(" Randomito: %i\n Cant_tickets: %i \n\n", randomito, cant_tickets);
		printf("--- RANDOMITO: %i ---\n", randomito);
		int rango_tickets = 0;
		int cant_elem = ready.size(); 
		for(int j = 0; j<cant_elem; j++){ 
			if(randomito <= (rango_tickets + tickets[ready[j]])) {
				printf("--- GANADOR LOTERIA: %i ---\n", ready[j]);
				return ready[j];
			}
			
			rango_tickets += tickets[ready[j]];
		}
		
	}
	
	//printf("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n");
	quantum = 0;
	return -1;
}

void SchedLottery::compensationT(){
	if(quantum != 0){
		
		float f = ((float) quantum) / def_quantum;
		//printf("TICKETS ANTES: %i --- ", tickets[current_pid()]);
		tickets[current_pid()] = 3*(int) (base_ticket / f);
		
		//printf("QUANTUM: %i --- DEF_QUANTUM: %i --- COMPENSACION: %f --- TICKETS: %i\n", quantum, def_quantum, f, tickets[current_pid()]);
		printf("QUANTUM: %i --- DEF_QUANTUM: %i --- COMPENSACION: %f --- TICKETS: %i\n", quantum, def_quantum, f, tickets[current_pid()]);
	}
	//~ int cant_tickets = tickets[current_id()];
	//~ def_quantum  
	
	//cant_tickets -= tickets[current_pid()]; 
	//tickets[current_pid()] = (int) tickets[current_pid()] / f;//	(int) cant_tickets/f;
	//cant_tickets += tickets[current_pid()];	
}
